From b4ac7763beeabbc040395b860560d92014ce5005 Mon Sep 17 00:00:00 2001 From: "mjw@wray-m-3.hpl.hp.com" Date: Thu, 28 Apr 2005 08:59:31 +0000 Subject: [PATCH] bitkeeper revision 1.1327.2.12 (4270a5f3EYQTq9cfFkGDYjjJ7ckjuw) Fix problem with Python 2.4 caused by change in error behaviour of dircache.listdir. In 2.4 it throws an error on non-existent directories instead of returning an empty list. See 'Porting to 2.4' in the Python release notes. Signed-off-by: Mike Wray --- tools/python/xen/xend/XendDB.py | 37 ++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/tools/python/xen/xend/XendDB.py b/tools/python/xen/xend/XendDB.py index 6a27e65b58..1701b5183b 100644 --- a/tools/python/xen/xend/XendDB.py +++ b/tools/python/xen/xend/XendDB.py @@ -20,6 +20,12 @@ class XendDB: self.dbpath = os.path.join(self.dbpath, path) pass + def listdir(self, dpath): + try: + return dircache.listdir(dpath) + except: + return [] + def filepath(self, path): return os.path.join(self.dbpath, path) @@ -52,21 +58,37 @@ class XendDB: return self.savefile(fpath, sxpr) def savefile(self, fpath, sxpr): + backup = False fdir = os.path.dirname(fpath) if not os.path.isdir(fdir): os.makedirs(fdir) + if os.path.exists(fpath): + backup = True + real_fpath = fpath + fpath += ".new." + fout = file(fpath, "wb+") try: - t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) - fout.write("# %s %s\n" % (fpath, t)) - sxp.show(sxpr, out=fout) - finally: - fout.close() + try: + t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + fout.write("# %s %s\n" % (fpath, t)) + sxp.show(sxpr, out=fout) + finally: + fout.close() + except: + if backup: + try: + os.unlink(fpath) + except: + pass + raise + if backup: + os.rename(fpath, real_fpath) def fetchall(self, path): dpath = self.filepath(path) d = {} - for k in dircache.listdir(dpath): + for k in self.listdir(dpath): try: v = self.fetchfile(os.path.join(dpath, k)) d[k] = v @@ -84,8 +106,7 @@ class XendDB: def ls(self, path): dpath = self.filepath(path) - return dircache.listdir(dpath) - + return self.listdir(dpath) -- 2.30.2